package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wnxy.pawnbackgroundsystem.commons.DateUtil;
import com.wnxy.pawnbackgroundsystem.commons.JwtUtils;
import com.wnxy.pawnbackgroundsystem.commons.ServiceImplUtil;
import com.wnxy.pawnbackgroundsystem.dao.AssessMapper;
import com.wnxy.pawnbackgroundsystem.dao.GoodsMapper;
import com.wnxy.pawnbackgroundsystem.entity.Assess;
import com.wnxy.pawnbackgroundsystem.entity.Goods;
import com.wnxy.pawnbackgroundsystem.entity.User;
import com.wnxy.pawnbackgroundsystem.param.assessParam.AssessParam;
import com.wnxy.pawnbackgroundsystem.service.IAssessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 评估历史表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-17
 */
@Service
@Slf4j
public class AssessServiceImpl extends ServiceImpl<AssessMapper, Assess> implements IAssessService {

    @Resource
    private AssessMapper assessMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public IPage<Assess> findAllAssessByPage(Integer goodsId, Integer pageSize, Integer current) {

        log.info("查询评估历史业务层：查询所有商品信息接受到的数据goodsid{},当前页{}，每页{}条,",goodsId,pageSize,current);
        Page page = new Page(current,pageSize);

        QueryWrapper<Assess> qw = new QueryWrapper<>();
        qw.eq("goods_id",goodsId);

        IPage<Assess> assesssPage = assessMapper.selectPage(page,qw);

        log.info("查询评估历史业务层传回控制层的参数：{}",assesssPage.getRecords());

        return assesssPage;
    }
    public Integer assessTure(AssessParam assessParam){
        log.info("鉴定为真品，assessParam:{}",assessParam);

        Assess assess = new Assess();
        BeanUtils.copyProperties(assessParam,assess);
        //获取当前登录用户名
        User user = ServiceImplUtil.getJwtUtilUser(JwtUtils.AUTH_TOKEN_NAME);
        log.info("从jwt中拿去登录人的信息user:{}",user);
        assess.setGoodsAssessPerson(user.getUserName());
        Long systemTimeStamp = DateUtil.getSystemTimeStamp();
        assess.setGoodsAssessTime(systemTimeStamp);
        int identifyTure = assessMapper.insert(assess);

        Goods goods = new Goods();
        goods.setGoodsId(assess.getGoodsId());
        goods.setGoodsPrice1(assess.getAssessMoney1());
        goods.setGoodsStatus("已评估");
        int i = goodsMapper.updateById(goods);
        return identifyTure;

    }
}
